package votd_notification;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import database.helpers.VOTD_Table;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.PowerManager;
import android.widget.Toast;
import androidhive.dashboard.R;

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {

	final public static String ONE_TIME = "onetime";
	private VOTD_Table votdTable;
	String refSCr = "nothing";
	String scripture = "nothing";
//	public AlarmManagerBroadcastReceiver(VOTD_Table votdTab,Context contex) {
//		votdTable= votdTab;
//		context=contex;
//		// TODO Auto-generated constructor stub
//	}
	public AlarmManagerBroadcastReceiver() {
		// TODO Auto-generated constructor stub
	}
	@Override
	public void onReceive(Context context, Intent intent) {
		 PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
         PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
         //Acquire the lock
         wl.acquire();

         //You can do the processing here update the widget/remote views.
         Bundle extras = intent.getExtras();
         StringBuilder msgStr = new StringBuilder();
         
         if(extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)){
        	 msgStr.append("One time Timer : ");
         }
         Format formatter = new SimpleDateFormat("hh:mm:ss a");
         msgStr.append(formatter.format(new Date()));
         votdTable = new VOTD_Table (context);
         showNotification(context);
//         Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show();
         
         //Release the lock
         wl.release();
         
	}
	public void SetAlarm(Context context)
    {
        AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
        intent.putExtra(ONE_TIME, Boolean.FALSE);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
        //After after 30 seconds
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60*1000 * 1, pi); 
    }

    public void CancelAlarm(Context context)
    {
        Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }
    public void setOnetimeTimer(Context context){
    	AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
        intent.putExtra(ONE_TIME, Boolean.TRUE);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
    }
    private void getVOTD(Context paramContext) {
		// TODO Auto-generated method stub
		Cursor c = votdTable.getAllVOTDs();
		String wasRead;
		if (!c.equals(null))
			c.moveToFirst();
	while (!c.isAfterLast() ) {
			wasRead = c.getString(4);
			if (wasRead.equalsIgnoreCase("false"))
			{
			refSCr = c.getString(2);
			scripture = c.getString(3);
			votdTable.UpdateReadStatus(wasRead, c.getString(0));
		
			votdTable.close();
			return;
			}
			refSCr = "Finished";
			scripture = "Finished";
			c.moveToNext();
		}
    }
    private void showNotification(Context paramContext) {
    	getVOTD(paramContext);
    	
		Notification localNotification = new Notification(
				R.drawable.logo,
				paramContext.getString(R.string.app_name),
				System.currentTimeMillis());
		localNotification.flags = (0x10 | localNotification.flags);
		// PendingIntent that will start the EnterWeightActivity
		Intent intent1 =new Intent(paramContext, VOTD.class);
		intent1.putExtra(VOTD.INTENT_REF, refSCr);
		intent1.putExtra(VOTD.INTENT_MSG,"Zvaita");
		Toast.makeText(paramContext, scripture, Toast.LENGTH_LONG).show();
		PendingIntent localPendingIntent = PendingIntent.getActivity(
				paramContext, 0, intent1,0);
		localNotification.setLatestEventInfo(paramContext,
				paramContext.getString(R.string.app_name),
				"Verse Of The Day",
				localPendingIntent);
		// Retrieve a NotificationManager to show the notification
		((NotificationManager) paramContext.getSystemService("notification"))
				.notify(0, localNotification);
	}
}
